Could be mold
It is possible that you've got a mold infestation somewhere in the hot water system. We had this happen in a house we rented -- occasional blobs of black gunk would come out with the water.
Before you replace the tank, try "shocking" the system with a chlorine bleach/water mix (maybe 2-4% bleach). Run the hot water till you are sure the tank is full of the mix, then let it sit a little while. Run the faucets till the bleach smell is gone.
That should kill anything living in the tank, but if it is mold, you'll find bits of it coming out periodically afterward. You might still end up replacing the hot water heater.