Is the sentence logical?
It had been sunny for a week when the storm broke out.
Is this sentence logically valid and sound?
If I think of it, it looks like it means when the storm broke out, it was sunny. There couldn't be stormy weather and sunny weather at the same moment, could it? It's like being both black and white at the same time.
So it would be natural to say "It had been (or was) sunny for a week before the storm broke out", wouldn't it?
It is fine. At the point the storm broke out, it “had been” (past sense) sunny. Thus, the sunny and storm did not occur at the same time. It is the past perfect continuous tense.
https://learnenglish.britishcouncil.org/english-grammar-reference/past-perfect
It is difficult to construct a sentence such as this that makes sense without context.
Your re-phrase, for example, could be interpreted as meaning that there was a sunny week at some point, not necessarily the week immediately preceding, before the storm broke out.
Groucho says:
I was on safari. And one night I shot an elephant in my pajamas. How he got them on I still don't know.
Maybe you could write it as so.
It had been sunny for the whole week just before the storm broke out.
And then my dermatologist's receptionist will ask "at night too?" English is a weird language.