It's common for expats, digital nomads, and non-Spanish citizens to receive a salary from a foreign company while registering as autónomo in Spain. This is completely legal, and I know dozens of people doing it.
Things become more complicated if you're a Spanish citizen and set up a U.S. LLC to...