Western Digital external hard drive is constantly ejected
Someone reported a similar issue on an Apple discussion forum:

I tried that but the drive will still eject itself randomly at times.
I contacted Western Digital about this and they say it might be a
problem with my USB cord. So they're going to send me a new one.
Has
anyone else had this problem and found that it was due to a faulty USB
3.0 (compatible with USB 2.0) cord?

Another poster in the same thread commented:

Sounds like disk is on the way out. I would move or backup all data on
it.

You should first back up that drive before anything else. Contact WD to see if they can send you a new USB cord. It's a good idea to run some diagnostics on the drive to confirm that there are no disk errors. Testing on another computer would also be a good idea to narrow down to see if the issue is isolated to your machine or the drive.
If the drive is failing, you'll need to return/exchange it.