Is it safe for a woman to travel to Tangier by herself?
Safe, but it can eat up a lot of travel time that you can better use to see Spain.
Yes, it is safe but it’s worth combining Tangier with a visit to Southern Spain. You should definitely explore the magnificent cities of Granada, Malaga, Seville and Gibraltar.
You might consider saving Morocco for a time when you do it it properly. Having been to Tangier I can say it is like going to Tijuana and saying you've been to Mexico. The vendors in both the shops and on the street are pretty aggressive - I had one follow down 3 floors and to the street despite telling him no more than a dozen times.
Getting further into Morocco away from a port city is where you find more of the real Morocco.