My understanding of graduate school for history is that several years are spent learning about methods and the general history of your field of study, followed by time focused on a narrow field of study for your thesis. But I've noticed flaired contributors on this sub with seemingly unrelated fields of study and was curious about how this works in practice. So I have a few questions:
Building a house requires materials and tools. History is the same way--you need the historical data and modern research, but you also need the skills or tools to know what to do with them. That is the primary learning that takes place in grad school coursework, skills of finding sources and analyzing them.
A specialty is acquired by learning what tools are specific to that field (for example, reading 15th century handwriting is a tool for late medievalists that's the opposite of worthwhile for a 20th century Americanist to learn) but mostly by acquiring the materials to apply the tools to. And you do this by reading. A lot. A lot lot.
So in many ways, adding a specialty or switching is a matter of reading more. Since it's so much more reading to go from the ground up, evolution is more common than a direct switch. Paul Freedman is a great example. His first major book was on medieval peasants. Then he got interested in the medieval spice trade, wrote a book on that, and pursued that line of interest into becoming a food historian--including of modern America!
The other way people can seem to have multiple specialties is when you think what I'll call vertically versus horizontally. For example, my primary focus is the later Middle Ages (I wrote my MA thesis on the Reformation; my flair is legit). Let's call that my horizontal specialty--I have a pretty extensive base of knowledge about lots of aspects of the Middle Ages, even what doesn't pertain to my specific research. On the other hand, my research involves "vertical" categories like women's history, history of literature, communications history, religious history (in which I have a previous graduate degree as well, lol), etc. So to acquire some of the tools from those fields to help me in my own work, I also read scholarship in those topics from beyond the Middle Ages.
And ultimately, that's what it comes down to: you gain the ability to locate the important works on a topic, and then you read them. If you do this often enough, it's as good as a specialty. Most people don't because of the time involved.
If you're interested in "adding a specialty," a great way to do that is to track down a reading list from someone's PhD comprehensive exams. They'll give you a rigorous, systematic foundation in the key works of scholarship in the field, important questions, methodological concerns, and of course the actual historical data. If you're interested, University of Wisconsin-Madison's history department has a really nice set of lists available to download.