Or, to be less snide: Kin selection, but with memetics rather than genetics. To be more simple: cultures whose behavior patterns perpetuate the survival of their members, survive themselves.
Economic theory says we're better off letting other countries do the stuff they're better than us at doing. If Shanghai makes better cars, it helps both them and us "perpetuate survival" if we just go ahead and buy their cars and spend our effort (and our savings!) doing something else.
Efficient markets mention nothing of capital, and the tendency for it to self-perpetuate. I can trade my oranges for your turnips and we both benefit, but what happens when you eat my oranges and I sell your turnips for a pear orchard? You could argue that rational actors would tend toward investment rather than consumption, and I'd point to a negative/extremely low U.S. savings rate. Humans behave like rational actors, but we aren't. Or perhaps we are, but our utility functions are incomputable, or have conflicting interests with different weights at different times of the day, etc.
So, revering markets for their sensitivity, speed, and efficiency, I am nonetheless suspicious of them. Markets didn't take us to the moon, but I'm glad we went. Markets allow the smart to get wealthy, and the stupid to get poor. We should remember the latter as we celebrate the former.
Suppose we takes some cash and use it to buy stuff from Japan. What that means is we give them mere pieces of paper with writing on it and they give us in return actual goods that are useful to us. Now we have some goods, and they have some paper. What do they DO with the paper? There are two options. Either they keep it forever, or they spend it.
If they were to just keep the money (and never spend it on anything), that works out GREAT for us, because it's a lot cheaper to print extra money than to make extra stuff. If the money never came back, we could print more with impunity and never worry about inflation or ruining the exchange rate.
But in all likelihood, they'll spend that money. So again we have two options: Either they spend it on US goods and services directly or - more likely, and most relevant to your question - they spend it on goods from some other country and THAT country (or another one, after a few more hops) in turn spends it on US goods and services. The money is only valuable to the outside world because they can spend it, and when they do that it comes back to US in exchange for some good or service that we make that SOMEBODY wants.
In the case of Japan, we buy their stuff for dollars, they spend those dollars on oil from some OPEC country (they'd actually rather buy oil from Alaska - it'd be a lot cheaper to ship but we've stupidly made it ILLEGAL to sell it to them - and then the oil-producing country in turn buys stuff from us.
So when we trade, our purchasers get a good deal on the products they buy aboard and that turns into demand for other products we sell and investment in our economy at large.
The dislocation caused by switching "making stuff" between two places means that people will be made unemployed and die in poverty.
It doesn't, for instance, counter-balance the trade benefit with an assessment of the societal cost of our production moving overseas.
You can look only as far as stockholders to see how diverse a corporation's ownership is, or you can take the next step and see how it changes hands at three or four levels. You'd be surprised.
The money dispersal you speak of goes to things like parts - most of which don't originate from the US. Which is EXACTLY why we need corporate headquarters here.
Nationalism/Racism => "People are more deserving if [they live in X/are of ethnicity X]."
I met a guy in a bar who was upset that a BP gas station opened up in his community. He wished it was something like Shell. I said "Oh, you mean the Dutch company, Royal Dutch Shell?"
That drove the point home to him, but the deeper point is that the shareholders and employees are of a company like that are global. Even if they weren't, the people that the shareholders and employees of that company give money to (the people they buy toilet paper, cars, and food from) can be and are of any nationality also.
Take a $20 bill and see how many hands touch it in a year. Take that same 20 dollars as a ledger entry and see how many countries have possession over a fraction of it in that same year. It's staggering.
"Buy American" is a complete illusion.
Nationalism is more like, "hey, I live in Tampa Bay so the Buccaneers are the best!" It's groundless presumption that someone or something is better or more deserving because you are close to it. It's a pernicious cognitive bias.
It's not so much that one thing is better as it is that you are more able to help those closest to you, in roughly the order yourself - family - community - country.
Without some degree of nationalism it is hard to justify things like universal healthcare, the welfare state or even politics on a smaller than global level. We invest in our communities because they invest in us.