Database API and Security
For input, saving to the DB, Database API takes care of it, automatically prevents SQL injection when using dynamic query. Since your code is using dynamic query, you're all good.

If you were to use static query you have to make sure you use placeholders as the docs mention.

For output, to prevent XSS, you'll want to use a template file, and print your value via Twig. Which auto escapes everything by default. If you don't use Twig, then you need to use the appropriate API function.