I've been interested in the effects of Napoleon's wars in Europe recently, and this is one thing I don't quite understand. When Napoleon removed the HRE in 1806, I believe 300 German integrated with one another into about 39 or so? I believe that could be one factor. Did revolutionary ideas from France spread or what?
I've answered this question comprehensively from numerous different perspectives. The answers below should answer your questions but if you have any further confusions I'm here to help :) All are more or less relevant to your question but I tried to order them further in relevance to your specific question. Ultimately though I think you're moreso asking the origins of German nationalism and what happened between 1806 and 1871 that had them all unify -- that's not a simple answer and requires some reading. The below posts, together, should act as a good primer for understanding why.
How and why did people accept nationalism when it first emerged?
The details of German unification // Why did Catholic Bavaria unify with Protestant Prussia?
Ultimately though I think /u/Sid_Burns answers your specific question most concisely in one of his posts:
The collapse of the Holy Roman Empire was a disaster for any state not named Prussia or Austria because the smaller German states relied on the legal framework of the Empire to prevent them from being gobbled up by their neighbours. So the South German states had a choice, either join Germany where they have to deal with Prussia but otherwise are protected or remain independent with two significantly more powerful states on their border. I would also add that Bismarck made the German federation sound far more appealing and equal than it actually was. I mean the Federal council, where each state has a say on what bills are passed sounds great in theory, but in practice Prussia was powerful enough to dominate the council.
To clarify a bit more; the fall of the HRE did not necessarily lead to more German nationalism; it created a Europe in which already burgeoning nationalist thought could grasp for real change; in other words a perfect storm was created where at the precise time where German nationalism was bubbling down from the upper classes, things like literacy and industrialization were taking off, the HRE was disbanded and a world was created where the German people were, once again, completely vulnerable to more powerful neighbors. This manifested (as I go into depth in my last link) in the yearning for a centralized, unified state to protect all Germans.
Further as the Germanic states were mediatized under Napoleon (as you mention, 300+ to 39) it allowed governments to industrialize and centralize their governments. Rather than having the border gore that was the Holy Roman Empire, where every no name knight had a few square miles of land in the middle of nowhere, there were strictly defined, mostly logical, simple States. It allowed economic growth to flourish but most importantly it allowed the rulers of these states to implement more socially progressive ideas and further mobilize their populaces both ideologically and literally; this is one of the central arguments of William Carr's opening chapter in his work The Origins of the Wars of German Unification. German nationalism existed before mediatization but mediatization created functional states which could actually get something of worth done and represent something of a modern state.
As for the revolutionary ideas from France; sort of. They were super big in Bavaria but everywhere else, even Bavaria to an extent, German nationalism was fundamentally dissimilar from French nationalism. I go into this in one of my links above but French nationalism was centered around abstract ideas like constitutionalism, natural rights, humanism, philosophy, etc. German nationalism, on the contrary, was centered around shared culture and history (usually focusing on the tragedy part; "Europe's battleground"). German nationalism was not expressed through glorious revolution and vying for a perfect state but, as one 1830's contemporary put it to paraphrase, the idea that the beating heart of German culture beats in every German's heart in every corner of Germany. It expressed itself more in traditional clothing and beer halls and songs and sports clubs than anything else.