Common advice to writers is to “write what you know.” Which I agree with. But people often misunderstand what “write what you know” means.
“Write what you know” doesn’t mean you can only write about your life or things you’ve personally done or experienced.
“Write what you know” means you have to know what you write about. You could know things from personal experience, but you could also know things through research.
For example, to write about outer space, you don’t have to be an astronaut. But you should have researched the science of space and read books, watched documentaries, and listened to interviews with astronauts, astronomers, and astrophysicists.
The same applies to other countries, time periods, and characters, including those of different genders, races, cultures, and occupations. You can write about people who act and think differently than you…if you take the time to genuinely learn about how actual people like that think and act. Which today, with the internet, you can easily do. Everyone in the world has access to everyone else in the world. A Google search gives you access to all the world’s knowledge. You can write about any person, place, or thing, as long as you do so respectfully, making sure you know what you’re writing about and taking the care to get it right.
So yes, you should write what you know. But you can, with enough research, know anything.