This practice is valid. When a Muslim recites the testimony of faith (shahada), it is an act of worship. It is not just recited due to fear of polytheism. A ruling of polytheism is only applied upon an action done with the knowledge of it being a polytheistic action.
Reciting the testimony of faith (shahada) is a righteous form of remembrance. The scholars mention that its repetition has many virtues. Among them are;