The captain of a ship flying the Spanish flag must be an EEA national.
"Article 253. Vessel endowments. [...]
2. The master and the first deck officer of national ships must have the nationality of a Member State of the European Economic Area, except in the cases in which it is established by the maritime Administration that these jobs must be carried out by citizens of nationality Spanish for implying the effective exercise in a regular way of public power prerogatives that do not represent a very small part of its activities. The rest of the crew, in the case of merchant ships, must be of Spanish nationality or of some other member state of the European Economic Area, at least 50 percent. [...]" (Royal Legislative Decree approving the Consolidated Text of the Law on State Ports and the Merchant Marine (2/2011, of September 5), last amended on 31 December 2020, Article 253(2) [google translation])
Royal Legislative Decree approving the Consolidated Text of the Law on State Ports and the Merchant Marine (2/2011, of September 5), last amended on 31 December 2020, Article 253(2) 
[<a href="https://www.boe.es/buscar/act.php?id=BOE-A-2011-16467">Decree</a>]