Is it safe to give wordpress directories ownership to www-data?

No, it is not safe as it means that any security breach might end up in your code being changed.

Alternatives:

  1. There is nothing wrong in using SFTP to upload changed plugins. If it is something that might take long time, you can manually put wordpress into maintenance mode by adding a file named .maintenance at the wordpress root directory and removing it after that (For core upgrades I just upload to different directories and then rename them).

  2. You can set up an FTP server which is blocked from the outside world but can accept traffic from localhost, and configure a user in it that will have permission to write to your wordpress directories, and use this user credentials when upgrading/installing.