MEXICO CITY—Three years after President Trump vowed to build a wall on the U.S. border with Mexico to stop illegal immigrants, a much more effective obstacle has emerged: Mexican President Andrés Manuel López Obrador.

Earlier this week, Mexico deployed hundreds of troops from its new National Guard to halt a caravan of some 2,000 Central American migrants who crossed a shallow river on Guatemala’s border with Mexico seeking to make their way to the U.S. Mexican television broadcast video of the guardsmen using plastic shields...