Roared on at nearly packed Cornella-El Prat stadium, last year's Euro semifinalists dominated from the start but had to wait until the 75th minute to open the scoring as Ferran Torres finished from close range from a pass by substitute Yeremy.
As the game looked to be heading for a draw, Olmo received a short pass from Jordi Alba and curled a shot into the top corner from the edge of the box.
Spain next take on Iceland in A Coruna on Tuesday as they continue the build-up to this year's World Cup in Qatar, which will be played in November and December.
More than 35,000 of the 40,000 tickets available for what would normally be a fairly low interest friendly were sold.
After years of tension between the Madrid central government and Catalonia separatist movement there were concerns about how the crowd would react but any worry disappeared from the start.
The fans joined in with the melody of Spain's national anthem, which has no words, and were fully supportive of their team, creating a great atmosphere at Espanyol's stadium.
The political situation, less turbulent than in the recent past, helped ease the tensions but the makeup of the core of the team also played a part, with four starters from Barcelona.
Goalkeeper David Raya also began his career with a team who trained five minutes from the stadium, while Spain coach Luis Enrique had success as player and manager at Barca, who he took to a Champions League, LaLiga and Copa del Rey Treble in 2015.
"The crowd was amazing, they deserved a win," Olmo told reporters after the match. "We dominated and it was a good test. If we are playing friendlies now it's because we are qualified for the World Cup so it's says a lot.
Missing several forwards through injury, Luis Enrique used Ferran Torres as Spain's target man, but it took him until 15 minutes from the end to turn their near 80% possession into a goal before Olmo fired the winner after their defensive lapse.