It's a marketing thing. Readers who liked your first book are going to want another book "just like that one, but different." And if your first book is a romance and your second one is a mystery, some percentage of the readers of the first book will throw the second one at a wall and never read any future book. On the other hand, if you figure out what your core story is -- what it is that really interests you, not necessarily the genre, but the heart of the story (e.g., community or sex or mystery or the absurdity of life or slices of life or childhood or whatever), and you develop a following based on "I like her voice and I'm interested in her core story of ____," then you can, maybe, bend the genre lines a bit.
Dara Joy did this some years ago (before she crashed and burned), but there was a core element (steamy scenes with particularly hunky heroes, before erotic romance really got off the ground), and the same voice, regardless of which genre she was in.
It's also possible that part (a small part, admittedly) of why she crashed and burned had to do with spreading too wide a net, instead of catering to a defined audience.
Books are, in essence, commodities, and they benefit from branding (of the author) much the same as any other product does. It's part of why authors who write across genres do so at their peril, and often take a pen name to do so. Jayne Ann Krentz reportedly almost tanked her career back in the 70s (or 80s?) when she did some futuristic/paranormal books, and had to wait until recently before she could go back to them, writing as Jayne Castle.
Anyway, it's always a matter of what the author wants out of her career. There are two basic options: establish yourself as a brand that readers will be able to identify (e.g., she writes romantic comedy set on islands), to maximize reader identification; or write books in a number of genres (which will then be shelved in different areas of the bookstore, so it's not like someone can say "Oh, I liked that book, so I ought to like the one right next to it by the same author.") and never get any cumulative effect from the prior books.
If you'll be miserable writing in the same genre, then don't do it. But be aware that it will, most likely, seriously reduce your income potential.
JD
JD