I think one of the reasons it stayed together is that the natives were culturally weak. Unlike places like Mexico or Peru with strong native cultures that didn't want to share the same country.
Also the whites were able to mix with the natives and blacks, unlike South Africa. According to genetic studies most of Brazilian DNA is European, not African or Native. So it's not like there are a bunch of black tribals bloc voting for their shaman or whatever happens in SA. I remember OP posted a topic where some blacks rioted because they were zulus and their mayor was xhosa or something.
I think Latin America is intrinsically violent, look at Venezuela. Also not a drug hub and it's even worse. Some of these central american countries are also terrible. It's also different from Mexico because the drug dealers here are not very organized, people just love killing each other it seems.
Mexican and Peruvian native cultures were obliterated, the nobles were baptized, converted and given nobility and the rest were worked to near death most of the time, couple with diseases and some good old book burning by the inquisition.
That being said, Spanish-American holdings were divided into three viceroyalties.
Viceroyalty of New Spain, New Granada and Peru.
So it would make sense if there were 3 Hispanic nations, but alas, infighting and more infighting. Everyone wanted to be king of their own turf and pay taxes to nobody.
Every single Hispanic country is the result of a lot (or won depending on the side) civil war or sedition war.
Brazil managed to stay whole in a time where i would presume every Portuguese upstart would vie for their own kingdom.
My guess is that Brazil knew how to govern itself more as a nation since at a point Brazil was the seat of the Portuguese crown.
Meanwhile Mexico was filled with Creoles (american born europeans), who were always ruled by a Spanish born individual, so when they rebelled after France invaded Spain and saw the Spanish rule as illegitimate, they just didnt know how to fucking govern as a single country.
Its weird because in the case of Mexico, it was european vs europeans fighting for power without a clear cause and it took an educated indian to beat everyone and actually found the Mexican republic.