For a long time the Iberian peninsula was divided into a (variable) number of kingdoms - Navarre, Asturias, Aragon, Castile - most of which had variable borders. Spain was a cultural concept rather than a political one. At various times different kingdoms were connected by marriages between their royal families, whicn in time would mean that different kingdoms were ruled by relatives - sometimes close relatives.
In the fifteenth century the Kingdoms of Castile and Aragon were (in practice, if not formally) united by the marriage of their respective monarchs, Isabella and Ferdinand, and towards the end of the century they jointly conquered the Kingdom of Granada. The effectively united most of what is now Spain. Their descendants were Kings of Castile and Aragon (and often other places as well); they were often referred to as Kings of Spain, although they didn;t formally claim that title.
For example, in a Treat of 1604 the then King style of “Philip the Third, by the grace of God, king of Castile, Leon, Aragon and the Two Sicilies, Jerusalem, Portugal, Navarre, Granada, Toledo, Valencia, Galicia, the Majorcas, Seville, Cordoba, Corsica, Murcia, Guinea, Algarve, Gibraltar, the Canary Islands, also of the Eastern and Western Indies, and the islands and terra firma of the Ocean Sea, archduke of Austria, duke of Burgundy and Milan, count of Habsburg, Barcelona, and Biscay, and lord of Molina, etc.” in full and “Philip III, King of the Spains” for short.