A U.S. law enforcement official and a U.S. intelligence source, speaking on condition of anonymity, said the attack might well have been carried out in response to recent Islamic State video messages urging would-be militants to attack trains and other public transport.