Yes, you can, provided you don't hit any other creature with the secondary effect
War Caster Says : The third benefit of the War Caster feat says (PHB, p. 170; emphasis mine):
- When a hostile creature's movement provokes an opportunity attack from you, you can use your reaction to cast a spell at the creature, rather than making an opportunity attack. The spell must have a casting time of 1 action and must target only that creature.
As long as the only creature getting hit by the spell is the one that provoked the Opportunity Attackopportunity attack, it is legal.
In response to a very similar question on Twitter about using eldritch blast (as a character of 5th level or higher) with War Caster, rules designer Jeremy E. Crawford tweeted thistweeted the following.:
The 3rd benefit of War Caster requires you to target only the moving creature. Eldritch blast works as long as all the beams target it.
All Emphasis Mine