Each country is allowed one naturalized citizen on their roster for international competition. The limit is in place to prevent countries from basically buying up all the top talent just to get medal. The USA has by far the most depth of talent so you often see Americans who probably won't ever make our own national team (or who don't want to endure the internal politics of the selection process, or who want a nice paycheck...) play for other countries. Astou Ndour Fall, another second tier WNBA post who has bounced around to a few teams like Megan, used to play for Spain. Megan replaced her. Unlike Megan,
Ndour Fall did have actual ties to Spain though having moved their in her teens for basketball development. Ndour Fall won a silver medal with Spain at the 2016 Olympics.
As far as how Spain ended up with Megan specifically-- I assume Spain looked at their own roster, saw a need for a post who can score around the basket, know that Megan has put up huge numbers in European leagues and has WNBA experience, and reached out to Megan's agent.