I could be off base here, but it seems to me that Bavaria and its adjacent regions have more in common, culturally, with Austria than they do with Protestant majority Northern Germany - especially in a historical context. Why would they - and similar South German Catholic states like Baden - choose to side with something called the "North German Federation" over their cousins and fellow Catholics in Austria?
Well Bavaria didn't exactly "choose" to join the North German Federation. To understand how the North Germany Confederation came to be we have to start with the Holy Roman Empire. It consisted of a lot of loose fiefs, baronies, kingdoms, etc as well as the Austrian Empire. In 1806 Napoleon defeats and destroys this feudal system and wounds national pride (especially Prussian national pride, who had always been - and will keep being - proud of their military prowess. With the defeat of Napoleonic France, the rulers of various European powers met in Vienna (at the Congress of Vienna) to discuss three main problems:
How to control France/ensure France can't conquer most of Europe again
the security of Italy
the security of Germany. This last question/problem was solved with the foundation of the German Confederation - basically (grain of salt please) the same as the Holy Roman Empire: a loose collection of smaller to larger territories (fiefs, kingdoms, etc). The German Confederation was chaired by Austria.
At this point Austria still has influence of the rest of Germany but this changes.
Remember the wounded national pride? Well this wounded national pride and the common enemy Napoleon helped bring the people of the different German territories together and helped start to build a national identity. This nationalist sentiment was also strengthened by Napoleon, who had - temporarily - partly erased the previous territorial distinctions. This nationalist movement wasn't happy with the German Confederation as it still didn't really unify Germany. I'm going to gloss over what happens here but this nationalist movement eventually leads to a revolution in 1848. Disclaimer: the nationalist movement also supports democratic and liberal sentiments, this isn't the nationalist movement we see later, pre-World War II. In fact, the Revolution of 1848 is sparked by uprisings and revolutions in other countries and sympathy for nationalist movements in other countries are widespread.
While this is happening, Prussia is cementing its influence as the second large power in "Germany." In 1818 Prussia creates a Customs Union throughout the Confederation that helps it sell its goods - Prussia at the time becoming more and more industrialized and was producing a lot of goods for exportation. Almost all members of the German Confederation joined this Union, except for Austria, which pursued a protectionist economic policy at the time. This is the first step in the distancing of Austria from other German states.
Back to the revolution. The revolution takes place, delegates from every state meet in Frankfurt in the National Assembly of St. Paul's Church. Different Factions have different opinions on different topics (obviously) but the one that matters is the question of "what is even Germany". Remember, this was a nationalist movement, so they basically all wanted a Germany made up of Germans, excluding territories with other ethnicities. This included all of Austria's territories in Eastern Europe (Hungary, etc). Two solutions arose, the Greater German Solution (Germany + Austria without its non-German territories) and the Lesser German Solution (Germany without Austria). Obviously both solutions sucked for Austria. The Lesser German Solution wins the votes, as it is easier to realize. This obviously helps to distance Austria (again) but doesn't ever come to effect as the Assembly decides to offer the "German Crown" to the Prussian King who refuses, basically delegitimizing the entire Assembly and causing the Revolution to lose popular support and eventually fail. This obviously doesn't end the nationalist/democratic movement in Germany and more and more territories adopt constitutions and some form of popular representation. Enter Bismarck. When the Prussian diet won't allow a military budget, King Wilhelm I appoints Bismarck as the Prussian Minister President. Bismarck manhandles the diet and gets the representatives back under control (he gives a speech called "Blood and Iron" - "Eisen und Blut" in German that basically highlights the importance of the military for Prussia - remember the national pride? - and basically explains to the diet, quite directly, that the King has more power than they do and can override them. Bismarck brings about Austro-Prussian dualism, where Prussia and Austria work together to conquer territories that don't belong to Germany but are populated by Germans (one of the problems discussed by the Frankfurt National Assembly). This is 1864:
Prussia and Austria conquer parts of Germany controlled by Denmark (Schleswig and Holstein) and the two conquerors split the new territories (which are geographically located far away from Austria). Disagreements between Prussia and Austria arise about how to rule these new territories, Austria break the Gastein Convention by putting Schleswig-Holstein under a Federal Diet, Prussia occupies Denmark. This Gastein Convention basically was an agreement between Austria and Prussia of how to govern these new territories. Bismarck proposes a new constitution for the territories, Austria finds these unacceptable and mobilizes troops. Bismarck officially declares the end of this Convention and the Austro-Prussian War starts. Austria fights alongside the southern German states (Bavaria included) as well as Hanover (further to the north, in Lower Saxony). A couple of smaller northern States fight alongside Prussia's well-equipped Army under von Moltke. The term Blitz-Krieg was coined and the war won in seven weeks. Bismarck, not wanting to overly aggravate Austria, didn't annex any Austrian territory and gave mild terms. He did however annex Austrian allies in the North (Hanover) and created the Northern German Confederation which included all states above the Main. Austria (obviously) wasn't included which put an end to Austro-Prussian dualism. This is where Bavaria comes into play. Bavaria was also defeated by Prussia. When Bismarck established another Customs Union (basically like the old one, except it excluded Austria and Prussia had more control) with a parliament, the southern German States were "allowed" to join and, in 1867, forced to accept the Customs Union Parliament. This brought about economic unity in Germany (excluding Austria). So Bavaria is now heavily influenced and basically controlled by Prussia.
and eventually the German state
Well, Bismarck could have stopped here, but he wanted a unified Germany. It is important to remember that Bismarck is a real political badass because what he does next is incredible.
Napoleon III is unwilling to accept the German Unification. Moreover the Spanish Queen is deposed in 1868 and the Spanish offer the Crown to a relative of the Prussian King (with a claim to the throne obviously). The French are now doubly pissed at Germany, once because of the German Unification and now because they don't want a Prussian puppet as the head of Spain (they don't want Prussia surrounding them) and because they have their own claim to the Spanish Throne. So the French send an ambassador to convince Wilhelm I to renounce the offer, but he declines the immediate offer. Cue Bismarck. Bismarck publishes a Telegram that makes it look like Wilhelm insulted the French Ambassador which outrages the French populace and leaves Napoleon III no choice but to declare War on Prussia. Did I already mention Blitz-Krieg? Prussia's superior military and quick mobilization allowed it to end the War in 6 months (they captured Napoleon III in only three months). The Franco-Prussian war was vital to German Unification as it saw Germany fighting a common enemy (France) together on a united front. Once in Versailles (in the Hall of Mirrors) Wilhelm I was crowned the German Emperor (over Germany, including Bavaria)
Tldr: Bavaria didn't choose to end up in the sphere of Prussia, but Bismarck happened and so Bavaria basically (sort of but not really) got annexed and economically manhandled by Prussia.
Sources:
Disclaimer: I'm not a historian and this is all from last year's class. I glossed over a lot of processes (almost everything) that I deemed not so important in answering the question, but hope I could answer your question and meet the high standards of this sub.
Edit: I apologize for grammar/spelling/phrasing in advance, don't really have time to read through all this so I hope this is at least understandable.