Have you ever lived anywhere as a foreigner, OP? Assuming you are British, you are likely to have had an easy ride even if you have. That isn’t always the case and where you come from can make the difference too. For example, living as a British person in France in my twenties really wasn’t a big deal and gave me an idea of how it felt to be an expat but as someone from one of the most privileged countries in the world I wasn’t at a disadvantage. My experience would likely have been very different if I had come from somewhere less privileged in a global context. Or if my command of the language was an issue.
Similarly, if you come from somewhere that isn’t deemed “popular” for whatever reason that might also make it extra wearisome and loaded a question that one might rather avoid getting into on a regular basis, particularly with strangers whose motives and reaction are uncertain. Even if you are from somewhere “popular”, it can probably get tedious to keep having the same conversations over and over and listen to people showing off their special knowledge of your home place because they went there on holiday once or whatever, or their neighbour’s granny’s hairdresser’s goat went our with someone from there once.
A relatively harmless example would be my American partner being constantly bleated at over American politics and the Iraq war etc when we lived in France in the noughties. He got so fed up of having to apologise for being American and having to justify himself as not being a war-monger and a villain because people felt entitled to preach self-righteously at him. It gets very tiresome to be put through your paces over your place of origin on a regular basis or being expected to defend yourself over political issues and engage in discussion that you might not want to.
I have had a relatively easy time of it wherever I have lived and being white British has no doubt helped because as previously mentioned I am lucky to come from a privileged place in the world. The one exception was a place that I won’t name because it is outing but I wasn’t entitled to work, (not the US where I had a lovely time!) and it was the tiniest insight into how it feels to be a second class citizen. I say tiny because my experience was generally positive but I occasionally encountered a bit of sniffiness as an outsider and it made me feel (momentarily) like dirt and I realised that if I felt like that with all my white, British, middle-class educated privilege it must feel really draining to be treated like an outsider everyday.
Everyone who is saying they don’t understand why people get offended by this, I would just say again that it depends. A Danish person who is here to study, or a Polish person here to work or a Yazidi girl who has fled persecution might all feel differently about being asked. Your motives might be kindness and a wish to show acceptance in asking but they may also not be up for gratifying your curiosity or feeling like a conduit for demonstrating how right on you for being interested in them, which could in itself be construed as slightly patronising. But again it all depends on context. I am just trying to say that asking a Brit in Spain where they are from is not necessarily the same as asking a refugee. The last taxi driver who took me to the airport had arrived here as an orphan from Sri Lanka and for whatever reason he chose to talk about it on our journey but I didn’t pry and it was his choice.
The bottom line in my view is that it is fine to ask where people are from in most circumstances where it can’t be misconstrued. But usually you might have a vague idea before you ask and based on that you might decide whether it would me more polite and appropriate to let them bring it up in their own time. People like to be regarded as people first and foremost, not foreigners and not objects of curiosity there to gratify another person’s need to be seen as “nice” and “interested”. I am not saying you are like this and that could be an ungenerous way of putting it but basically if you are nosey even if it’s well-intentioned, you sometimes run the risk of offending people.